<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    

<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="《Python数据分析》学习记录, 炫杉">
    <meta name="description" content="这是阅读《Python for Data Analysis》的读书笔记。

第一章 准备工作不用准备了。🙂🙂🙂要啥有啥。
第二章 引言list 手动统计from collections improt defaultdict
def g">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>《Python数据分析》学习记录 | 炫杉</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/css/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>

</head>

<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="container">
            <div class="nav-wrapper">
                <div class="brand-logo">
                    <a href="/" class="waves-effect waves-light">
                        
                        <img src="/medias/logo.png" class="logo-img hide-on-small-only">
                        
                        <span class="logo-span">炫杉</span>
                    </a>
                </div>
                <a href="#" data-activates="mobile-nav" class="button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>Index</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>Tags</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>Categories</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>Archives</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>About</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="https://oo1.win" class="waves-effect waves-light">
            
            <i class="fa fa-star"></i>
            
            <span>Bookmark</span>
        </a>
    </li>
    
    <li>
        <a id="toggleSearch" class="waves-effect waves-light">
            <i id="searchIcon" class="mdi-action-search"></i>
        </a>
    </li>

</ul>

<div class="side-nav" id="mobile-nav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">炫杉</div>
        <div class="logo-desc">
            
            提着鸟笼的老头站在一旁拍我肩膀 我猜他想的应该和我一样
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                Index
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                Tags
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                Categories
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                Archives
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                About
            </a>
        </li>
        
        <li>
            <a href="https://oo1.win" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-star"></i>
                
                Bookmark
            </a>
        </li>
        
        
    </ul>

    <div class="social-link"><a href="https://github.com/ixsim" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
    <i class="fa fa-github"></i>
</a>

<a href="mailto:chenjiayin1990@163.com" class="tooltipped" target="_blank" data-tooltip="邮件:chinalixs@qq.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>

<a href="http://wpa.qq.com/msgrd?v=3&uin=6956820&site=qq&menu=yes" class="tooltipped" data-tooltip="QQ: 6956820" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>
</div>
</div>

            </div>
        </div>

        
    </nav>
</header>



<div class="bg-cover post-cover" style="background-image: url('/medias/featureimages/12.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        《Python数据分析》学习记录
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1,
    #articleContent h2,
    #articleContent h3,
    #articleContent h4,
    #articleContent h5,
    #articleContent h6 {
        padding-top: 76px;
        margin-top: -76px;
    }

    #articleContent h1 {
        line-height: 3.5rem;
    }

    #articleContent h2 {
        line-height: 3.2rem;
    }

    #articleContent h3 {
        line-height: 2.8rem;
    }

    #articleContent h4 {
        line-height: 2.5rem;
    }

    #articleContent h5 {
        line-height: 2.2rem;
    }

    #articleContent h6 {
        line-height: 1.9rem;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }
</style>
<div class="row">
    <div class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            
            <div class="article-tag">
                
                <a href="/tags/Note/" target="_blank">
                    <span class="chip bg-color">Note</span>
                </a>
                
                <a href="/tags/Data-Analysis/" target="_blank">
                    <span class="chip bg-color">Data Analysis</span>
                </a>
                
                <a href="/tags/Python/" target="_blank">
                    <span class="chip bg-color">Python</span>
                </a>
                
            </div>
            
            <div class="post-info">
                
                <span class="post-cate">
                    <i class="fa fa-bookmark fa-fw icon-category"></i>
                    
                    <a href="/categories/Python/" class="post-category" target="_blank">
                        Python
                    </a>
                    
                </span>
                

                <span class="post-date">
                    <i class="fa fa-clock-o fa-fw"></i>2018-03-21
                </span>
            </div>
        </div>
        <hr>
        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>这是阅读《Python for Data Analysis》的读书笔记。</p>
<a id="more"></a>
<h1 id="第一章-准备工作"><a href="#第一章-准备工作" class="headerlink" title="第一章 准备工作"></a>第一章 准备工作</h1><p>不用准备了。🙂🙂🙂要啥有啥。</p>
<h1 id="第二章-引言"><a href="#第二章-引言" class="headerlink" title="第二章 引言"></a>第二章 引言</h1><h2 id="list-手动统计"><a href="#list-手动统计" class="headerlink" title="list 手动统计"></a>list 手动统计</h2><pre class="line-numbers language-python"><code class="language-python"><span class="token keyword">from</span> collections improt defaultdict
<span class="token keyword">def</span> <span class="token function">get_count</span><span class="token punctuation">(</span>sequence<span class="token punctuation">)</span><span class="token punctuation">:</span>
    counts <span class="token operator">=</span> defaultdict<span class="token punctuation">(</span>int<span class="token punctuation">)</span>
    <span class="token keyword">for</span> x <span class="token keyword">in</span> sequence<span class="token punctuation">:</span>
        counts<span class="token punctuation">[</span>x<span class="token punctuation">]</span>  <span class="token operator">+=</span> <span class="token number">1</span>
    <span class="token keyword">return</span> counts
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>本章内容仅仅是向读者展示。跳~</p>
<h1 id="第三章-IPython"><a href="#第三章-IPython" class="headerlink" title="第三章 IPython"></a>第三章 IPython</h1><p>目前使用的IDE是Spyder，是集成了IPython的。值得一学。</p>
<h2 id="内省"><a href="#内省" class="headerlink" title="内省"></a>内省</h2><ul>
<li><p>变量前后加? 可查看该对象的通用信息</p>
<pre class="line-numbers language-python"><code class="language-python">  a?
  Type<span class="token punctuation">:</span>        int
  String form<span class="token punctuation">:</span> <span class="token number">10</span>
  Docstring<span class="token punctuation">:</span>  
  int<span class="token punctuation">(</span>x<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> integer
  int<span class="token punctuation">(</span>x<span class="token punctuation">,</span> base<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> integer
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
</ul>
<ul>
<li>如果对象是一个函数或实例方法。其docstring会被显示</li>
<li>?? 还将显示出该对象的源码</li>
</ul>
<ul>
<li><p>搜索IPython命名空间</p>
<pre class="line-numbers language-shell"><code class="language-shell">np.*load*? #列出NumPy顶级命名空间中含有“load”的所有函数
np.load
np.loads
np.loadtxt
no.pkgload
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
</ul>
<h3 id="run"><a href="#run" class="headerlink" title="%run"></a>%run</h3><pre class="line-numbers language-Python"><code class="language-Python">%run filename.py #运行这个文件
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>文件在一个空的命名空间中运行。此后该文件定义的全部变量就可以在IPython Shell中访问了。</p>
<p><strong>Note</strong><br>访问 Interactive 命名空间的变量：<br><code>%run -i</code></p>
<h3 id="中断执行的代码"><a href="#中断执行的代码" class="headerlink" title="中断执行的代码"></a>中断执行的代码</h3><p><code>Ctrl + C</code> </p>
<h3 id="执行剪贴板中的代码"><a href="#执行剪贴板中的代码" class="headerlink" title="执行剪贴板中的代码"></a>执行剪贴板中的代码</h3><p><code>%paster</code><br>执行后，剪贴板中的<strong>一切</strong>文本，在shell中以<strong>整体</strong>的形式执行。<br><code>%cpaste</code><br>执行后，你想粘贴多少就粘贴多少。按下<code>Ctrl + C</code>终止<code>%cpaste</code></p>
<h3 id="键盘快捷键"><a href="#键盘快捷键" class="headerlink" title="键盘快捷键"></a>键盘快捷键</h3><p>我是个没有快捷键会死星人。<br>记几个常用的吧：</p>
<table>
<thead>
<tr>
<th style="text-align:right">Ctrl+</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right">A</td>
<td style="text-align:center">光标至行首</td>
</tr>
<tr>
<td style="text-align:right">E</td>
<td style="text-align:center">光标至行尾</td>
</tr>
<tr>
<td style="text-align:right">K</td>
<td style="text-align:center">从光标往后全删</td>
</tr>
<tr>
<td style="text-align:right">U</td>
<td style="text-align:center">从光标往前全删</td>
</tr>
<tr>
<td style="text-align:right">L</td>
<td style="text-align:center">清屏</td>
</tr>
</tbody>
</table>
<h3 id="魔术命令"><a href="#魔术命令" class="headerlink" title="魔术命令"></a>魔术命令</h3><p>魔术命令是以百分号为前缀的命令</p>
<p>常用</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>%quickref</td>
<td>显示IPython快速参考</td>
</tr>
<tr>
<td>%magic</td>
<td>显示所有magic文档</td>
</tr>
<tr>
<td>%debug</td>
<td></td>
</tr>
<tr>
<td>%hist</td>
<td>历史</td>
</tr>
<tr>
<td>%paste %cpaste</td>
<td></td>
</tr>
<tr>
<td>%reset</td>
<td>删除interactive命名空间中的全部变量/名称</td>
</tr>
<tr>
<td>%run script.py</td>
<td>执行脚本文件script.py</td>
</tr>
<tr>
<td>%time statement</td>
<td>报告statement的执行时间</td>
</tr>
<tr>
<td>%timeit statement</td>
<td>多次执行statement返回平均执行时间</td>
</tr>
<tr>
<td>%who %who_Is %whos</td>
<td>显示interactive命名空间中定义的变量</td>
</tr>
</tbody>
</table>
<h2 id="命令历史"><a href="#命令历史" class="headerlink" title="命令历史"></a>命令历史</h2><p>上下键能一个个切换<br><code>Ctrl + P/N</code>能根据你输入向前/后匹配历史</p>
<h3 id="输入输出变量"><a href="#输入输出变量" class="headerlink" title="输入输出变量"></a>输入输出变量</h3><p><code>_</code>和<code>__</code>分别是最后两个输出</p>
<p>输入变量：<br>第27行的输入：<code>_i27</code></p>
<p><code>%hist</code> 显示全部或部分输入输出历史，并可选是否带行号</p>
<h2 id="与OS交互"><a href="#与OS交互" class="headerlink" title="与OS交互"></a>与OS交互</h2><p>自认为还用不到。调用os Shell ，目录书签。以后研读。</p>
<h2 id="软件开发工具"><a href="#软件开发工具" class="headerlink" title="软件开发工具"></a>软件开发工具</h2><p>本章就这样了。快快开始学干货吧~</p>
<h1 id="第四章-Numpy"><a href="#第四章-Numpy" class="headerlink" title="第四章 Numpy"></a>第四章 Numpy</h1><h2 id="ndarray"><a href="#ndarray" class="headerlink" title="ndarray"></a>ndarray</h2><h3 id="创建"><a href="#创建" class="headerlink" title="创建"></a>创建</h3><table>
<thead>
<tr>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>array</td>
<td>将输入数据(list,tuple,etc)转为ndarry</td>
</tr>
<tr>
<td>asarray</td>
<td>与array区别：当输入是个ndarry时，不进行复制</td>
</tr>
<tr>
<td>arange</td>
<td>不解释</td>
</tr>
<tr>
<td>ones\ones_like</td>
<td>全1。ones可设shape。而ones_like是传入一个数组a，生成的数组与其同shape。</td>
</tr>
<tr>
<td>zeros\zeros_like</td>
<td>同上</td>
</tr>
<tr>
<td>empty\empty_like</td>
<td>不解释</td>
</tr>
<tr>
<td>eye\identity</td>
<td>NxN单位矩阵（对角1，其余0）</td>
</tr>
</tbody>
</table>
<p><strong>array和asarray区别</strong>：都可以将结构数据转化为ndarray，但是主要区别就是当数据源是ndarray时，array仍然会copy出一个副本，占用新的内存，但asarray不会。</p>
<h3 id="索引和切片"><a href="#索引和切片" class="headerlink" title="索引和切片"></a>索引和切片</h3><ul>
<li>数组切片是原是数组的视图。这意味着：<strong>这些数据不是被复制的，视图上的任何修改都会被直接反映到原数组上。</strong><br>（我虽然改的是切片，但其实原数组也改了）</li>
<li>如果你想得到的是副本也不是视图。要显式复制：<code>arr[5:8].copy()</code></li>
</ul>
<h3 id="布尔型索引"><a href="#布尔型索引" class="headerlink" title="布尔型索引"></a>布尔型索引</h3><pre class="line-numbers language-python"><code class="language-python">arrary <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token boolean">True</span><span class="token punctuation">,</span><span class="token boolean">False</span><span class="token punctuation">,</span><span class="token boolean">True</span><span class="token punctuation">]</span><span class="token punctuation">,</span>dtype <span class="token operator">=</span> bool<span class="token punctuation">)</span>
data<span class="token punctuation">[</span>arrary<span class="token punctuation">]</span> <span class="token comment" spellcheck="true">#输出第1、3行</span>
<span class="token comment" spellcheck="true">#还可以与切片混用</span>
data<span class="token punctuation">[</span>arrary<span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment" spellcheck="true">#输出1、3行的前两列</span>
<span class="token comment" spellcheck="true">#索引还可以同时满足多个条件</span>
mask <span class="token operator">=</span> <span class="token punctuation">(</span>names <span class="token operator">==</span> <span class="token string">'Bob'</span><span class="token punctuation">)</span> <span class="token operator">|</span> <span class="token punctuation">(</span>names <span class="token operator">!=</span> <span class="token string">'Will'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">#mask就是一个bool型数组</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="花式索引"><a href="#花式索引" class="headerlink" title="花式索引"></a>花式索引</h3><pre class="line-numbers language-python"><code class="language-python">arr<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token comment" spellcheck="true"># 输出第5、1、7行</span>
arr<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token comment" spellcheck="true"># 输出倒数1、5行</span>
arr<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token comment" spellcheck="true">#输出坐标(1,0) (5,3)(4,1)三个数</span>
<span class="token comment" spellcheck="true">#那如何利用索引输出该行列呢</span>
<span class="token comment" spellcheck="true">#要用切片配合</span>
arr<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token comment" spellcheck="true">#输出1、5、4行的0、2、1列</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><strong>Note</strong> 花式索引跟切片不一样。不是视图是复制。</p>
<h3 id="转置"><a href="#转置" class="headerlink" title="转置"></a>转置</h3><p>转置返回的是<strong>视图</strong></p>
<h3 id="通用函数"><a href="#通用函数" class="headerlink" title="通用函数"></a>通用函数</h3><p>ufuns(元素级数组方法)<br>函数<br><code>abs、fabs</code><br>计算整数、浮点数或复数的绝对值。对于非复数值,可以使用更快的fabs<br><code>sgrt</code><br>计算各元素的平方根。相当于arr <strong> 0.5<br><code>square</code><br>计算各元素的平方。相当于arr </strong> 2<br><code>exp</code><br>计算各元素的指数e<br><code>log、log10、log2、log1p</code><br>分别为自然对数(底数为e)、底数为10的log、底数为2的log、log(1+x)<br><code>sign</code><br>计算各元素的正负号:1(正数)、0(零)1(负数)<br><code>ceil</code><br>计算各元素的 ceiling值,即大于等于该值的最小整数<br><code>floor</code><br>计算各元素的for值,即小于等于该值的最大整数<br><code>rint</code><br>将各元素值四舍五入到最接近的整数,保留 dtype<br><code>modf</code><br>将数组的小数和整数部分以两个独立数组的形式返回<br><code>isnan</code><br>返回一个表示“哪些值是NaN(这不是一个数字)”的布尔型数组<br><code>isfinite、 isinf</code><br>分别返回一个表示“哪些元素是有穷的(非inf,feiNaN)”或“哪些元素是无穷的”的布尔型数组<br><code>cos、cosh、sin、Sinh、tan、tanh</code><br>普通型和双曲型三角函数<br><img src="http://p66eruxmw.bkt.clouddn.com/15221187685515.jpg" alt=""></p>
<h3 id="将条件逻辑表述为数组运算"><a href="#将条件逻辑表述为数组运算" class="headerlink" title="将条件逻辑表述为数组运算"></a>将条件逻辑表述为数组运算</h3><p><code>np.where</code></p>
<pre><code>result = [(x if c else y) for x,y,c in zip(xarr,yarr,cond)]
#对大数组不快。无法用于多维数组。不如直接用where
resul =  np.where(cond,xarr,yarr)
#将正值设为2
np.where(arr&gt;0,2,arr)
</code></pre><h3 id="基本数组统计方法"><a href="#基本数组统计方法" class="headerlink" title="基本数组统计方法"></a>基本数组统计方法</h3><p>有个axis参数。横着是1  竖着是0<br>sum求和<br>mean平均数<br>std、var标准差方差<br>min\max<br>argmin\argmax 最大、小值的索引</p>
<p>cumsum 所有元素累积和<br>cumprod 所有元素累计积<br>这两个函数还会产生中间结果<br>Ex：</p>
<pre><code>import numpy as np
arr = np.arange(0,9).reshape(3,3)
print(arr)
[[0 1 2]
 [3 4 5]
 [6 7 8]]
print(arr.cumsum(0)) #竖着加
[[ 0  1  2]
 [ 3  5  7]
 [ 9 12 15]]
print(arr.cumprod(1)) #横着乘
[[  0   0   0]
 [  3  12  60]
 [  6  42 336]]
</code></pre><h3 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h3><p><code>unique(x)</code><br>计算x中的唯一元素,并返回有序结果<br><code>intersected(x, y)</code><br>计算x和y中的公共元素,并返回有序结果<br>unionid(x, y)<br>计算x和y的并集,并返回有序结果<br><code>in 1d(x, y)</code><br>得到一个表示“x的元素是否包含于y”的布尔型数组<br><code>setdiff1d(x, y)</code><br>集合的差,即元素在x中且不在y中<br><code>setxor1d(x, y)</code><br>集合的对称差,即存在于一个数组中但不同时存在于两个数组中的元素</p>
<h2 id="文件输入输出"><a href="#文件输入输出" class="headerlink" title="文件输入输出"></a>文件输入输出</h2><pre><code>arr = np.arrange(10)
np.save(&#39;some_array&#39;,arr) # 被存为.npy文件
np.load(&#39;a.npy&#39;) #读取

arch = np.savez(&#39;archive.npz&#39;,a=arr, b=arr) #多个数组压缩至一个npz文件
#加载时会对各个数组进行延迟加载
arch[&#39;b&#39;] #可单独访问
</code></pre><h2 id="线代"><a href="#线代" class="headerlink" title="线代"></a>线代</h2><p><code>diag</code><br>以一维数组的形式返回方阵的对角线(或非对角线)元素,或将一维数组转换为方阵(非对角线元素为0)<br><code>dot</code><br>矩阵乘法<br><code>trace</code><br>计算对角线元素的和<br><code>det</code><br>计算矩阵行列式<br><code>eig</code><br>计算方阵的本征值和本征向量<br><code>inv</code><br>计算方阵的逆<br><code>pinv</code><br>计算矩阵的 Moore- Penrose伪逆<br><code>qr</code><br>计算QR分解<br><code>svd</code><br>计算奇异值分解(SVD)<br><code>solve</code><br>解线性方程组AX=b,其中A为一个方阵<br><code>lstsq</code><br>计算Ax=b的最小二乘解</p>
<h2 id="随机数"><a href="#随机数" class="headerlink" title="随机数"></a>随机数</h2><p><code>seed</code><br>确定随机数生成器的种子<br><code>permutation</code><br>返回一个序列的随机排列或返回一个随机排列的范围<br><code>shuffle</code>对一个序列就地随机排列<br><code>rand</code><br>产生均匀分布的样本值<br><code>randint</code><br>从给定的上下限范围内随机选取整数<br><code>randn</code><br>产生正态分布(平均值为0,标准差为1)的样本值,类似于 MATLAB接口<br><code>binomial</code><br>产生二项分布的样本值<br><code>nomal</code><br>产生正态(高斯)分布的样本值<br><code>beta</code><br>产生Beta分布的样本值<br><code>chisquare\gamma\uniform</code>卡方、Gamma、01分布</p>
<h2 id="范例"><a href="#范例" class="headerlink" title="范例"></a>范例</h2><p>例子不错。而然我并不想记。不错不错第四章结束~喜大普奔迎接pandas</p>
<h1 id="第五章-pandas"><a href="#第五章-pandas" class="headerlink" title="第五章 pandas"></a>第五章 pandas</h1><h2 id="数据结构"><a href="#数据结构" class="headerlink" title="数据结构"></a>数据结构</h2><h3 id="Series"><a href="#Series" class="headerlink" title="Series"></a>Series</h3><ul>
<li><p>一维数组对象。<br>（可传入字典建立，就像把字典列成表）</p>
</li>
<li><p>在算数运算中会自动对齐不同索引的数据。</p>
</li>
<li><p>Series对象本身和其索引都有一个name属性。<br>就等于这个表有个名 <code>obj.name</code>。索引有个名<code>obj.index.name</code>。</p>
</li>
</ul>
<h3 id="DataFrame"><a href="#DataFrame" class="headerlink" title="DataFrame"></a>DataFrame</h3><ul>
<li>通过索引返回的列是<strong>视图</strong></li>
</ul>
<p>创建方法<br><img src="http://p66eruxmw.bkt.clouddn.com/15223741416892.jpg" alt=""><br>P123表待插入</p>
<h3 id="索引对象"><a href="#索引对象" class="headerlink" title="索引对象"></a>索引对象</h3><ul>
<li>Index对象是<strong>不可修改</strong>的</li>
</ul>
<h2 id="基本功能"><a href="#基本功能" class="headerlink" title="基本功能"></a>基本功能</h2><h3 id="重新索引"><a href="#重新索引" class="headerlink" title="重新索引"></a>重新索引</h3><pre class="line-numbers language-python"><code class="language-python">obj<span class="token punctuation">.</span>reindex<span class="token punctuation">(</span><span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token comment" spellcheck="true">#新索引</span>
    method<span class="token punctuation">,</span><span class="token comment" spellcheck="true">#差插值填充方式：=ffill/pad 向前填充 =bfill/backfill向后填充</span>
    fill_vaule，<span class="token comment" spellcheck="true">#缺失值填充</span>
    limit<span class="token punctuation">,</span><span class="token comment" spellcheck="true">#向前、后填充时的最大填充量</span>
    level<span class="token punctuation">,</span><span class="token comment" spellcheck="true">#没看懂</span>
    copy<span class="token comment" spellcheck="true">#默认True，无论如何都复制。设为FALSE则新旧相等就不复制了)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="丢弃"><a href="#丢弃" class="headerlink" title="丢弃"></a>丢弃</h3><p><code>obj.drop([&#39;d&#39;,&#39;c&#39;])</code><br><code>obj.drop([&#39;1&#39;,&#39;3&#39;],axis=1)</code>竖着丢 </p>
<p><code>drop</code>方法返回的是一个在指定轴上删除了指定值的新对象：<br><code>new_obj = obj.drop(&#39;c&#39;)</code></p>
<h3 id="索引选取过滤"><a href="#索引选取过滤" class="headerlink" title="索引选取过滤"></a>索引选取过滤</h3><p><code>obj[&#39;a&#39;:&#39;c&#39;]</code> 这是根据列名来 选取（过滤）列<br>与Python切片不同的是c是<strong>包含</strong>的。<br><code>obj.ix[val]</code> 这是根据行名来选取行。<br><code>obj.ix[:,val]</code> 选取列</p>
<p>从例子和选取方法上看，我认为使用之前还是用demo调试着进行选取吧。有点乱~</p>
<h3 id="算数运算和数据对齐"><a href="#算数运算和数据对齐" class="headerlink" title="算数运算和数据对齐"></a>算数运算和数据对齐</h3><p>不重叠的索引引入NA值</p>
<h3 id="DataFrame-和-Series-之间的运算"><a href="#DataFrame-和-Series-之间的运算" class="headerlink" title="DataFrame 和 Series 之间的运算"></a>DataFrame 和 Series 之间的运算</h3><p>将<strong>Series</strong>的索引匹配到<strong>DataFrame</strong>的列。然后沿着行一直向下广播。</p>
<p>如果想匹配行且在列上广播。则必须使用算数方法：</p>
<p><code>a.sub(series,axis=0)</code></p>
<h3 id="函数应用和映射"><a href="#函数应用和映射" class="headerlink" title="函数应用和映射"></a>函数应用和映射</h3><p>NumPy的ufuncs也可用于操作pandas对象</p>
<p>DataFrame的apply方法可以：将函数应用到各行或各列的一维数组上。</p>
<h3 id="排序和排名"><a href="#排序和排名" class="headerlink" title="排序和排名"></a>排序和排名</h3><p>sort_index方法可以按索引（字典序）排序。</p>
<p>到此为止吧。数据分析。有缘再见！</p>

            </div>
            <hr>

            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            <div class="reprint">
                <p>
                    <span class="reprint-tip">转载请注明: </span>
                    <a href="https://xuanshan.gitee.io" class="b-link-green">炫杉</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/Python/《Python数据分析》学习记录/" class="b-link-green">《Python数据分析》学习记录</a>
                </p>
            </div>
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="MTAyMC8yOTg2My82NDI5">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">上一篇</div>
            <div class="card">
                <a href="/Website/网页空白处点击弹出文字/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/16.jpg" class="responsive-img" alt="网页空白处点击弹出文字">
                        
                        <span class="card-title">网页空白处点击弹出文字</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">空白处点击鼠标，弹出文字的网页特效。

代码如下：
&lt;script type="text/javascript">
/* 鼠标特效 */
var a_idx = 0;
jQuery(document).ready(function($</div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2018-03-23
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Website/" class="post-category" target="_blank">
                                    Website
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/js/" target="_blank">
                        <span class="chip bg-color">js</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">下一篇</div>
            <div class="card">
                <a href="/Python/Python初探/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/17.jpg" class="responsive-img" alt="Python初探">
                        
                        <span class="card-title">Python初探</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">这是我刚开始学习Python时做的笔记，百度Python后先看到有廖雪峰的教程。就对着看了一遍。紧接着同学推荐了莫烦的教程，果然，这个教程上手更快。另外我认为Python作为一个脚本语言，很多东西是在学习的时候没法记忆的，勤查着点就够了。因</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2018-03-21
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Python/" class="post-category" target="_blank">
                                    Python
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Python/" target="_blank">
                        <span class="chip bg-color">Python</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>
    </div>
    <div class="col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title">目录</div>
            <div id="toc-content">

            </div>
        </div>
    </div>
</div>

<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingSelector: 'h1,h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1,h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });
    });
</script>
    

</main>

<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            &copy;<a href="https://o--o.win" target="_blank">炫杉2018</a> 
            <a href="https://hexo.io/" target="_blank">Hexo</a> 强力驱动
              主题 <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">matery</a>        </div>
        <div class="col s12 m4 l4 social-link"><a href="https://github.com/ixsim" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
    <i class="fa fa-github"></i>
</a>

<a href="mailto:chenjiayin1990@163.com" class="tooltipped" target="_blank" data-tooltip="邮件:chinalixs@qq.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>

<a href="http://wpa.qq.com/msgrd?v=3&uin=6956820&site=qq&menu=yes" class="tooltipped" data-tooltip="QQ: 6956820" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title">搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input" autofocus>
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/js/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>
</body>
</html>